//
//  SLPreferencesSetVC.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/23.
//

#import "SLAboutUsVC.h"
#import "SLPreferencesSetCell.h"

static  NSString *const kCellId = @"SLPreferencesSetCell";

@interface SLAboutUsVC ()<UITableViewDelegate,UITableViewDataSource>{
    
}
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation SLAboutUsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self confiDatas];
    [self setUpSubViews];
}

-(void)setUpSubViews{
    [self.view addSubview:self.tableView];
}

-(void)confiDatas{
    [self.dataArray removeAllObjects];
    NSArray *sectionTitle1 = @[kLocalString(@"App version")];
    NSString *version = [NSString stringWithFormat:@"Ver %@",kAppVersion];
    NSArray *sectionContent1 = @[version];
    
    NSArray *sectionTitle2 = @[kLocalString(@"Terms of use"),kLocalString(@"Privacy policy")];
    
    NSMutableArray *section1 = [NSMutableArray new];
    NSMutableArray *section2 = [NSMutableArray new];
    
    [sectionTitle1 enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *content = sectionContent1[idx];
        NSMutableDictionary *info = [NSMutableDictionary new];
        [info setObject:title forKey:@"title"];
        [info setObject:content forKey:@"content"];
        [info setObject:@"1" forKey:@"type"];
        [section1 addObject:info];
    }];
    
    [sectionTitle2 enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableDictionary *info = [NSMutableDictionary new];
        [info setObject:title forKey:@"title"];
        [info setObject:@"1" forKey:@"type"];
        [section2 addObject:info];
    }];
    
//    [_dataArray addObject:section1];
    [_dataArray addObject:section2];
}

-(void)refreshSubViews{
    [self confiDatas];
    [_tableView reloadData];
}

#pragma mark - ================<tableViewDataSource>===============
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *sectionArr = _dataArray[section];
    return sectionArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SLPreferencesSetCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *info = _dataArray[indexPath.section][indexPath.row];
    NSArray *section = _dataArray[indexPath.section];
    cell.line.hidden = indexPath.row == section.count-1;
    cell.titleLbl.text = info[@"title"];
    cell.contentLbl.text = info[@"content"];
    cell.contentLbl.textColor = kHexColor(@"#808080");
    cell.changeBtn.hidden = [info[@"type"] isEqualToString:@"1"];
    cell.list.hidden = ![info[@"type"] isEqualToString:@"1"];
    if (section.count == 1) {
        [cell.contentView setRoundedCorners:20 forCorners:UIRectCornerAllCorners frame:CGRectMake(0, 0, kScreenWidth-40, 50)];
    }else{
        [cell.contentView setRoundedCorners:0 forCorners:UIRectCornerAllCorners frame:CGRectMake(0, 0, kScreenWidth-40, 50)];
        if (indexPath.row == section.count-1) {
            [cell.contentView setRoundedCorners:20.0 forCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight frame:CGRectMake(0, 0, kScreenWidth-40, 50)];
        }else if (indexPath.row == 0){
            [cell.contentView setRoundedCorners:20.0 forCorners:UIRectCornerTopLeft | UIRectCornerTopRight frame:CGRectMake(0, 0, kScreenWidth-40, 50)];
        }
    }
    
    return cell;
}

#pragma mark - ================<UITableViewDelegate>===============
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headview = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (!headview) {
        headview = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"head"];
        headview.frame = CGRectMake(0, 0, kScreenWidth, 20);
        headview.contentView.backgroundColor = KClearColor;
    }
    return headview;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if (!footerView) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
        footerView.frame = CGRectMake(0, 0, kScreenWidth, 0.1);
        footerView.contentView.backgroundColor = KClearColor;
    }
    return footerView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *info = _dataArray[indexPath.section][indexPath.row];
    NSString *title = info[@"title"];
    if ([title isEqualToString:kLocalString(@"App version")]) {

    }else if ([title isEqualToString:kLocalString(@"Terms of use")]){
        NSString *url = [NSString stringWithFormat:@"%@/#/appsupport/secret/%@/iOS",URL_web,[ZCTool getLang]];
        RootWebViewController *webVc = [[RootWebViewController alloc] initWithUrl:url];
        webVc.titleStr = title;
        [self.navigationController pushViewController:webVc animated:YES];
    }else if ([title isEqualToString:kLocalString(@"Privacy policy")]){
        NSString *url = [NSString stringWithFormat:@"%@/#/appsupport/privacy/%@/iOS",URL_web,[ZCTool getLang]];
        RootWebViewController *webVc = [[RootWebViewController alloc] initWithUrl:url];
        webVc.titleStr = title;
        [self.navigationController pushViewController:webVc animated:YES];
    }
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, SafeAreaTopHeight,kScreenWidth-40,kScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight) style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 50;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellReuseIdentifier:kCellId];
        _tableView.backgroundColor = KClearColor;
    }
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}



@end
